package com.example.dazuoye.controller;

import com.example.dazuoye.mapper.SearchCarRepository;
import com.example.dazuoye.model.SearchCar;
import com.example.dazuoye.service.ManageService;
import com.example.dazuoye.service.UserService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class Managecontroller {
    @Autowired(required = true)
    private ManageService serv;
    @Autowired(required = true)
    private UserService userv;
    @Autowired
    private SearchCarRepository searchCarRepository;
    private UserService service;

    private String host;
    private String rootPath = "/etc";
    private String sonPath = "/456/";
    private static final Logger logger =  LoggerFactory.getLogger(FileController.class);
    @ResponseBody
    @RequestMapping(value="/uploadcarpic")
    public String ucp(@RequestParam(value="image") MultipartFile file,@RequestParam(value="carnum") String carnum)
    {
        if(file.isEmpty())
            return "文件为空";

        host = "https://www.hkzhaoscut.top";

        Filenamegen filenamegen = new Filenamegen();

        String originfilename = file.getOriginalFilename();
        String suffix = originfilename.substring(originfilename.lastIndexOf(".")+1);

        String fileName =  filenamegen.getfname()+"."+suffix;
        String filePath = rootPath+sonPath;
        logger.info("上传的文件路径"+filePath);
        logger.info("整个图片路径："+host+":" +sonPath+fileName);
        java.io.File dest = new File(filePath+fileName);
        String imgPath = (host+":" + sonPath +fileName).toString();
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try{
            file.transferTo(dest);

            System.out.println("success");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        serv.uploadcarpic(carnum,imgPath);
        return imgPath;
    }
    @ResponseBody
    @RequestMapping(value="/addnewcar")
    public String newcar(@RequestParam(value="carnum") String carnum, @RequestParam(value="color") String color,
                         @RequestParam(value="model") String model, @RequestParam(value="numseat") int numseat, @RequestParam(value="brand") String brand,@RequestParam(value = "carsite") String carsite)
    {
        //System.out.println(qrcode);
        serv.insertcar(carnum,color,model,"",numseat,brand,"",carsite);
        int c_id=serv.getcid(carnum);
        String title = "车牌:"+carnum+"颜色:"+color+"型号:"+model+"座位数"+numseat+"人"+"品牌"+brand+"地址位置:"+carsite;
        Long id = Integer.toUnsignedLong(c_id);
        SearchCar searchCar= new SearchCar(id,title,carnum,color,model,numseat,brand,carsite);
        searchCarRepository.save(searchCar);
        String qrcode = QrCodeUtils.creatRrCode(String.valueOf(c_id), 200, 200);
        serv.setqrcode(c_id,qrcode);
        return Dataimagegen.createdataimage(qrcode);
    }
   /* @RequestMapping(value="/uploadcarpic",method = RequestMethod.GET)
    public String u()
    {
        //System.out.println("aaaa");
        return "index";
    }
    @ResponseBody
    @RequestMapping(value="/uploadcarpic",method = RequestMethod.POST)
    public String uploadpic(@RequestParam(value="c_id") int c_id,@RequestParam(value="carpic") MultipartFile pic)
    {
        String fname=Filenamegen.getfname();
        return fname;
    }*/
    @ResponseBody
    @RequestMapping(value="/delcar")
    public int delcar(@RequestParam(value="c_id") int c_id)
    {
        serv.delcar(c_id);
        Long id = Integer.toUnsignedLong(c_id);
        searchCarRepository.deleteById(id);
        return 1;
    }
    @ResponseBody
    @RequestMapping(value="/getalluser")
    public JSONArray getalluser()
    {
        return JSONArray.fromObject(serv.getalluser());
    }
    @ResponseBody
    @RequestMapping(value="/deluser")
    public int deluser(@RequestParam(value="u_id") String u_id)
    {
        serv.deluser(u_id);
        return 1;
    }
    @ResponseBody
    @RequestMapping(value="/acceptuser")
    public String acceptuser(@RequestParam(value="u_id") String u_id)
    {
        serv.acceptuser(u_id);
        return "审核确定成功";
    }
    @ResponseBody
    @RequestMapping(value="/adminlogin")
    public int adminlogin(@RequestParam(value="username") String username,@RequestParam(value="password") String passwd)
    {
        String tmp=serv.getpasswd(username);
        //System.out.println(passwd);
        //System.out.println(tmp);
        if (tmp.equals(passwd)) return 1;
        return 0;
    }
    @ResponseBody
    @RequestMapping(value="/distributecoupon")
    public JSONObject di(@RequestParam(value="u_id") String u_id)
    {
        JSONObject m=new JSONObject();
        m.put("u_id",u_id);
        serv.distributecoupon(u_id);
        m.put("couponcount",String.valueOf(userv.getwalletinfo(u_id).getCoupon_count()));
        return m;
    }
    @ResponseBody
    @RequestMapping(value="/getqrcode") //获取qrcode by 车牌号
    public String qr(@RequestParam(value="carnum") String carnum)
    {
        return Dataimagegen.createdataimage(serv.quickgetcarinfo(carnum).getQrcode());
    }
}
